//
//  RadioTableView.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/10/5.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "RadioTableView.h"
#import "RadioTableViewCell.h"
#import "MJRefresh.h"
#import "NetEaseDataService.h"

@implementation RadioTableView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.delegate = self;
        self.dataSource = self;
        
        [self addRefreshAction];
        
        [self registerClass:[RadioTableViewCell class] forCellReuseIdentifier:kRadioTableViewCellId];
    }
    
    return self;
}

- (void)setRadioDataArray:(NSArray *)radioDataArray {
    if (_radioDataArray != radioDataArray) {
        _radioDataArray = [radioDataArray copy];
        [self reloadData];
    }
}


#pragma mark - refreshAction
- (void)addRefreshAction {
    //下拉事件,刷新最新数据
    self.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewestAction)];
    //并没有上拉事件
}

- (void)loadNewestAction {
    [NetEaseDataService readerRequestSize:20 block:^(id result) {
        if ([result isKindOfClass:[NSError class]]) {
            NSLog(@"视听页 电台 数据请求失败 %@", result);
            [self.header endRefreshing];
            return;
        }
//        NSArray* array = [result objectForKey:@"推荐"];
//        if (array.count) {
//            self.readerModelArray = array;
//        }
    }];
    [self.header endRefreshing];
}

#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _radioDataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 10)];
    view.backgroundColor = [UIColor grayColor];
    view.alpha = 0.3;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 230;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RadioTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:kRadioTableViewCellId forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    
    NSDictionary* dataDic = _radioDataArray[indexPath.section];
    cell.radioDataDic = dataDic;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
//    //通过响应者链,找到readerVC
//    ReaderViewController* readerVC = [Tools className:@"ReaderViewController" fromResponder:self.nextResponder];
//    
//    if (readerVC) {
//        ReaderModel* model = [[ReaderModel alloc] initWithDataDic:_readerModelArray[indexPath.section]];
//        
//        NewsDetailViewController* newsDetailVC = [[NewsDetailViewController alloc] init];
//        if (model.readerId.length) {
//            newsDetailVC.newsId = model.readerId;
//            newsDetailVC.replyId = model.replyId;
//            [readerVC.navigationController pushViewController:newsDetailVC animated:YES];
//        }
//    }
}

@end
